package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.AddressDeleteFailException;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;

    //添加体检机构地址
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            addressService.add(address);
            return new Result(true, MessageConstant.ADD_ADRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADRESS_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return addressService.findPage(queryPageBean);
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        //给图片一个唯一的以.jpg结尾的命名
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String fileName = originalFilename.substring(index - 1) + UUID.randomUUID().toString();
        Jedis resource = null;
        try {
            //将图片上传到七牛云并保存到redis中
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        } finally {
            resource.close();
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

    }
    //编辑体检地址
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);
            return new Result(true, MessageConstant.EDIT_ADRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADRESS_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result edit(Integer id) {
        try {
            addressService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADRESS_SUCCESS);
        } catch (Exception e) {
            if(e instanceof AddressDeleteFailException){
                return new Result(false, "已有预约关联，请先删除预约！");
            }
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADRESS_FAIL);
        }
    }

}
